//
//  FireButtonNode.m
//  iFly
//
//  Created by User-23 on 12/8/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.
//

#import "FireButtonNode.h"
#import "VectorHelper.h"
#import "ProjectileFactory.h"
#import "CharacterNode.h"

@implementation FireButtonNode

+(instancetype) fireButtonOnPosition:(CGPoint) position inScene:(SKScene*) scene {
    FireButtonNode* button = [self spriteNodeWithImageNamed:fireButtonImage];
    
    //setting physical logic for the button
    button.name = @"fire";
    button.position = position;
    button.isButtonClicked = NO;
    button.zPosition = 1;
    button.weaponType = ProjectileTypeLaser;
    
    return button;
}

-(void) equipWeapon:(ProjectileType) type {
    self.weaponType = type;
    SKAction* wait = [SKAction waitForDuration:kTripleLaserDuration];
    SKAction* reequipNormalLaser = [SKAction runBlock:^{
        self.weaponType = ProjectileTypeLaser;
    }];
    SKAction* sequence = [SKAction sequence:@[wait,reequipNormalLaser]];
    [self runAction:sequence];
}

-(void)fire {
    if (!self.isButtonClicked && !self.parent.isPaused) {
        CharacterNode* character = (CharacterNode*)[self.parent childNodeWithName:@"character"];
        if (character.isDead || character.physicsBody.categoryBitMask == shieldCategory) {
            return; // if character is dead or shielded he cannot shoot
        }
        float scale = character.parent.scene.size.height/sceneScaling;
        CGPoint position;
        
        [character shoot];
        if (character.isShootingWithLeft){
            if (character.isForward) {
                position = [VectorHelper vectorAddPoint:character.position toPoint:CGPointMake(scale*handForwardShootWidthScaling, scale*handForwardShootHeightScaling)];
            }else {
                position = [VectorHelper vectorAddPoint:character.position toPoint:CGPointMake(scale*handBackwardsShootWidthScaling, scale*handBackwardsShootHeightScaling)];
            }
        }
        else{
            if (character.isForward) {
                position = [VectorHelper vectorAddPoint:character.position toPoint:CGPointMake(scale*handForwardShootWidthScaling, scale*handForwardShootHeightScaling)];
            }else {
                position = [VectorHelper vectorAddPoint:character.position toPoint:CGPointMake(scale*handBackwardsShootWidthScaling, scale*handBackwardsShootHeightScaling)];
            }
        }
        ProjectileNode* projectile = [ProjectileFactory createProjectileWithType:self.weaponType andPosition:position inScene:self.parent.scene];
        [projectile setScale:scale*2];
        
        SKAction* shoot = [SKAction runBlock:^{
            [projectile shoot];
            self.isButtonClicked = YES;
        }];
        SKAction* wait = [SKAction waitForDuration:0.15];
        SKAction* unlockButton = [SKAction runBlock:^{
            self.isButtonClicked = NO;
        }];
        //after each shot wait for 0.15 seconds before the next
        SKAction* sequence = [SKAction sequence:@[shoot,wait,unlockButton]];
        [self runAction:sequence];
    }
}

@end
